package com.tensquare.base.controller;

import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: tensquare-project
 * @description:
 **/
@RestController
@RequestMapping("/label")
@CrossOrigin  //允许跨域访问
public class LabelController {

    @Autowired
    private LabelService labelService;


    //处理前端提交CRUD请求

    /**
     * 标签保存
     * @param label
     * @return
     */
//    @RequestMapping(method = RequestMethod.POST)
    @PostMapping
    public Result save(@RequestBody Label label){
        labelService.save(label);
        return new Result(true, StatusCode.OK, "保存成功", null);
    }

    /**
     * 修改
     * @param labelId
     * @param label
     * @return
     */
    @PutMapping("/{labelId}")
    public Result update(@PathVariable String labelId, @RequestBody Label label){
        label.setId(labelId);
        labelService.update(label);
        return new Result(true, StatusCode.OK, "修改成功", null);
    }

    @DeleteMapping("/{labelId}")
    public Result deleteById(@PathVariable String labelId){
        labelService.deleteById(labelId);
        return new Result(true, StatusCode.OK, "删除成功", null);
    }


    @GetMapping("{labelId}")
    public Result findById(@PathVariable String labelId){
        Label label = labelService.findById(labelId);
        return new Result(true, StatusCode.OK, "查询成功", label);
    }

    @GetMapping
    public Result findAll(){
//        int i = 1/0;
        List<Label> list = labelService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 条件查询
     * @param map
     * @return 数据格式 数组
     */
    @PostMapping("/search")
    public Result search(@RequestBody(required = false) Map map){
        List<Label> list  = labelService.search(map);
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 条件分页查询
     * @param page 当前页
     * @param size 页大小
     * @param map 查询条件值
     * @return
     */
    @PostMapping("/search/{page}/{size}")
    public Result search(@PathVariable int page, @PathVariable int size, @RequestBody Map map){
        //Pageable对象中封装当前页，页大小
        Pageable pageable = PageRequest.of(page-1, size);
        //Page对象中封装总记录数，当前页记录
        Page<Label> pageData = labelService.search(map, pageable);
        //转json
//        pageData.getContent()
//        pageData.getTotalElements()  //-->{content:[],totalElements:13}

        //方式一
//        Map<String, Object> mapData = new HashMap<>();
//        mapData.put("total", pageData.getTotalElements());
//        mapData.put("rows", pageData.getContent());
        //方式二
        PageResult pageResult = new PageResult(pageData.getTotalElements(),  pageData.getContent());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);

    }





}
